<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件中的data为什么是一个函数</title>
</head>
<body>
    <div id="app">
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
    </div>

    <template id="tmp">
        <div>
            <h2>当前计数：{{counter}}</h2>
            <!-- 第一种写法 -->
            <!-- <button @click='counter++'>+</button> -->
            <!-- <button @click='counter--'>-</button> -->
            <!-- 第二种写法 -->
            <button @click='add'>+</button>
            <button @click='jian'>-</button>
        </div>
    </template>
</body>
<script src='../js/vue.js'></script>
<script>
    // 注册组件
    Vue.component('cpn',{
        template:'#tmp',
        data() {
            return {
                counter:0
            }
        },
        methods: {
            add(){
                this.counter++
            },
            jian(){
                this.counter--
            }
        },
    })

    let app = new Vue({
        el: '#app',
        data: {
             msg:'hello vue.js'
        },
        methods: {}
    })
</script>
</html>